package com.wangjiyuan.giftshuo.ui.fragment.other.detail;

import com.wangjiyuan.giftshuo.bean.detail.DetailBean;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wjy on 2016/11/20.
 */

public class DetailPresenter implements DetailContract.DetailPesenter {
    private DetailContract.DetailModel model;
    private DetailContract.DetailView view;
    private Subscription subscribe;

    public DetailPresenter(DetailContract.DetailView view) {
        model = new DetailModel();
        this.view = view;
    }

    @Override
    public void getData(String id) {
        Observable<DetailBean> date = model.getDate(id);
        subscribe = date.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<DetailBean>() {
                    @Override
                    public void onCompleted() {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {

                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onNext(DetailBean detailBean) {
                        view.getData(detailBean.getData());
                    }
                });
    }
}
